<?php
//======================================================================================
//
// Function: Save WAPMP100
//
// Programmer: JKJ
// Date : 2024-08-30
//
// Copyright Reeft A/S (c) - 2024
//======================================================================================
//======================================================================================
// General config
//======================================================================================
include "config/config.php";
//======================================================================================
// Get input
//======================================================================================
if (isset($_REQUEST["mode"])) $mode = $_REQUEST["mode"];
else $mode = '';
if (isset($_REQUEST["data"])) {
$data = $_REQUEST["data"];
} else {
$data = 'hovsa';
}
if ( $mode == '' ) {
echo "There's no action....1";
exit;
}
if ( $mode <> '*add' and $mode <> '*update' and $mode <> '*delete' ) {
echo "There's no action....2";
exit;
}
if ( $data == 'hovsa' ) {
echo "There's no data....";
exit;
}
//======================================================================================
// Convert from JSON
//======================================================================================
// Original JSON data
$jsonData = $data;
// Decode the JSON string into an associative array
$data = json_decode($jsonData, true);
// Initialize the new structure
$newJson = [
"header" => [
"APIKEY" => $DFT_API_KEY,
"mode" => $mode
],
"detail" => []
];
//======================================================================================
// Iterate through the original data to build the 'detail' section
// DELETE MODE
//======================================================================================
if ( $mode == '*delete') {
foreach ($data as $item) {
// Extract 'name' and 'value' from each item
$name = str_replace("confirm-delete-", "", $item['name']);
$value = $item['value'];
// Add to the 'detail' section
$newJson['detail'][$name] = $value;
}
}
//======================================================================================
// Iterate through the original data to build the 'detail' section
// "ANY" MODE
//======================================================================================
if ( $mode <> '*delete') {
foreach ($data as $item) {
// Extract 'name' and 'value' from each item
$name = str_replace("modal-input-", "", $item['name']);
$value = $item['value'];
// Handle special cases
// if ($name === "IPIMPSEQ") {
// // Convert 'IPIMPSEQ' value to integer
// $value = (int)$value;
// } elseif (strpos($name, "IPCPY") !== false) {
// // Change '*ADD' to '*REPLACE' for 'IPCPYOPT' field
// if ($name === "IPCPYOPT" && $value === "*ADD") {
// // $value = "*REPLACE";
// }
// }
// Add to the 'detail' section
$newJson['detail'][$name] = $value;
}
}
//======================================================================================
// Convert the new structure back to JSON
//======================================================================================
$newJsonString = json_encode($newJson, JSON_PRETTY_PRINT);
//======================================================================================
// Set the URL
//======================================================================================
$url = $DFT_WEBSERVICE_URL_DATAHUB . '/' . 'HUB_WA0361';
//======================================================================================
// Create a new cURL resource
//======================================================================================
$ch = curl_init($url);
// Attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $newJsonString);
// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt($ch, CURLOPT_HEADER, false); // we do not need headers
// curl_setopt($ch, CURLOPT_NOBODY, true); // we don't need body
// Execute the POST request
$result = curl_exec($ch);
$ary = curl_getinfo($ch);
// Close cURL resource
curl_close($ch);
//======================================================================================
// Send back the reply from the webservice
//======================================================================================
echo($result);
?>